package cn.tedu.interceptor.config;

import cn.tedu.interceptor.Inerceptor.MyInterceptor;
import cn.tedu.interceptor.Inerceptor.MyInterceptor1;
import cn.tedu.interceptor.Inerceptor.MyInterceptor2;
import cn.tedu.interceptor.Inerceptor.MyInterceptor3;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class Config implements WebMvcConfigurer {
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //将我们定义的拦截器MyInterceptor注册到SpringMVC框架中
        //这样一来，当SpringMVC启动时(对应的starter)就会将当前拦截器利用起来
        //如果拦截器注册时不指定路径，则是所有路径都需要经过拦截器
        /*
            http://localhost:8080/users/reg              拦截
            http://localhost:8080/users/login            拦截
            http://localhost:8080/index.html             拦截
            http://localhost:8080/123.html               拦截
         */
//        registry.addInterceptor(new MyInterceptor());


        //可以在注册拦截器时添加拦截路径，那么仅当该路径被请求时才会使用当前拦截器
        /*
            http://localhost:8080/users/reg              拦截
            http://localhost:8080/users/login            不拦截
            http://localhost:8080/index.html             不拦截
            http://localhost:8080/123.html               不拦截
         */
//        registry.addInterceptor(new MyInterceptor())
//                .addPathPatterns("/users/reg");


        /*
            http://localhost:8080/users/reg              拦截
            http://localhost:8080/users/login            不拦截
            http://localhost:8080/index.html             拦截
            http://localhost:8080/123.html               不拦截
         */
        //可以指定多个拦截路径，那么只有这些路径被请求时才会进行拦截
//        registry.addInterceptor(new MyInterceptor())
//                .addPathPatterns("/users/reg","/index.html");

        /*
            希望和用户相关的操作都拦截，其他的都不拦截
            http://localhost:8080/users/reg
            http://localhost:8080/users/login
            http://localhost:8080/users/currentUser
            http://localhost:8080/users/update
            http://localhost:8080/users/findById
            上述地址都拦截

            http://localhost:8080/123.html                  不拦截
            http://localhost:8080/index.html                不拦截
            http://localhost:8080/weibo/add                 不拦截

            在定义拦截地址时可以使用"**"进行通配。
         */
        //registry.addInterceptor(new MyInterceptor()).addPathPatterns("/users/**").excludePathPatterns("/users/login");
        //除了"/users/login"之外所有的
        registry.addInterceptor(new MyInterceptor1());
        registry.addInterceptor(new MyInterceptor2());
        registry.addInterceptor(new MyInterceptor3());
    }
}
